package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.GlobalParamModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 全局参数业务
 * @author zhaokf
 * @date 2013-04-20
 */
public class GlobalParamService extends BaseService
{
	/**
	 * 分页列出全局参数
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<GlobalParamModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("cfg_id") != null){
				where = " AND cfg_id=" + search.get("cfg_id");
			}
			if (search.get("cfg_name") != null){
				where = " AND cfg_name LIKE '%" + search.get("cfg_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<GlobalParamModel> paras = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM SUP_CONFIG " + where;

		try{
			paras = GlobalParamModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return paras;
	}



	/**
	 * 增加全局参数信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){

		Record record = new Record();
		record.set("cfg_id", Db.queryBigDecimal("select sup_config_seq.nextval from dual").intValue());
		record.set("cfg_name", params.get("cfg_name").toString());
		record.set("cfg_value", params.get("cfg_value").toString());
		boolean isupadded = Db.save("sup_config", "cfg_id",record);
		if(isupadded){
			return true;
		}
		return setError("你访问的全局参数信息已存在！");
	}

	/**
	 * 修改全局参数信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		Record record = new Record();
		record.set("cfg_id", params.get("cfg_id").toString());
		record.set("cfg_name", params.get("cfg_name").toString());
		record.set("cfg_value", params.get("cfg_value").toString());
		boolean isupadded = Db.save("sup_config", "cfg_id",record);
		if(isupadded){
			return true;
		}
		return setError("您访问的全局参数信息不存在！");
	}

	/**
	 * 删除全局参数信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		
		String no = params.get("cfg_id") == null ? "" : params.get("cfg_id").toString();
		String sql = "select * from sup_config where cfg_id=?";
		GlobalParamModel sm = GlobalParamModel.dao.findFirst(sql, no);
		if (sm != null)
		{
			sm.delete();
			return true;
		}
		return setError("您访问的全局参数信息不存在！");
	}
	/**
	 * 根据ID查询全局参数
	 * @param params
	 * @return
	 */
	public static GlobalParamModel get(Map<String,String> params){
		String no = params.get("cfg_id")==null?"":params.get("cfg_id").toString();
		GlobalParamModel gp = GlobalParamModel.dao.findFirst("SELECT * FROM SUP_CONFIG WHERE cfg_id=?", no);
		return gp;
	}
}

